package com.app.twth.ui.adjust.remeasure;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.app.twth.app.Constant;
import com.app.twth.base.BaseAndroidViewModel;
import com.app.twth.bean.BaseResponseEntity;
import com.app.twth.bean.LoadingBean;
import com.app.twth.net.JsonCallBack;
import com.app.twth.ui.checkin.FileUpload;
import com.app.twth.utils.DateUtil;
import com.app.twth.utils.ToastUtil;
import com.app.twth.utils.Utils;
import com.lzy.okgo.model.Response;


/**
 * ****************************************************************
 * 文件名称: LoginViewModel
 * 文件描述:
 * 注意事项:
 * 版权声明:
 * ****************************************************************
 */
public class ReMeasureViewModel extends BaseAndroidViewModel<ReMeasureDataModel> {
    public MutableLiveData<String> code = new MutableLiveData<>();
    public MutableLiveData<ReMeasureQr> reMeasureLiveData = new MutableLiveData<>();
    public MutableLiveData<FileUpload> fileUploadMutableLiveData = new MutableLiveData<>();
    public MutableLiveData<String> submitMutableLiveData = new MutableLiveData<>();

    public ReMeasureViewModel(@NonNull Application application) {
        super(application);
    }

    public void fileUpload(String containerNo, String fileName, String path, int type, int position, boolean isSubmit, boolean showDialog) {
        isLoading.setValue(new LoadingBean(showDialog, Constant.loading));
        errorResultCode.setValue("");
        String name = "remeasure/" + containerNo + "-" + fileName + "-" + DateUtil.getYMD() + "-" + System.currentTimeMillis() + ".jpg";
        mModel.upload(name, path, new JsonCallBack<BaseResponseEntity<String>>() {
            @Override
            public void onSuccessData(Response<BaseResponseEntity<String>> response) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                BaseResponseEntity<String> baseResponseEntity = response.body();
                if (baseResponseEntity == null) {
                    return;
                }
                if (baseResponseEntity.getCode() == null) {
                    return;
                }
                if (baseResponseEntity.getCode().equals("200")) {
                    FileUpload fileUpload = new FileUpload();
                    fileUpload.setType(type);
                    fileUpload.setPosition(position);
                    fileUpload.setPath(baseResponseEntity.data);
                    fileUpload.setSubmit(isSubmit);
                    fileUploadMutableLiveData.setValue(fileUpload);
                    return;
                }
                infoResultMsg = baseResponseEntity.getMessage();
                errorResultCode.setValue(baseResponseEntity.getCode());
            }

            @Override
            public void onError(String error) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                infoResultMsg = error;
                errorResultCode.setValue(error);
            }
        });
    }

    public boolean judgeType(String mold, String hint) {
        if (Utils.isEmpty(mold)) {
            ToastUtil.showToast(hint);
            return false;
        }
        if (Double.parseDouble(mold) <= 0) {
            ToastUtil.showToast(hint);
            return false;
        }
        return true;
    }


    public void tallyReMeasureSubmit() {
        isLoading.setValue(new LoadingBean(true, Constant.loading));
        errorResultCode.setValue("");
        mModel.tallyReMeasureSubmit(reMeasureLiveData.getValue(), new JsonCallBack<BaseResponseEntity<Object>>() {
            @Override
            public void onSuccessData(Response<BaseResponseEntity<Object>> response) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                BaseResponseEntity<Object> baseResponseEntity = response.body();
                if (baseResponseEntity == null) {
                    return;
                }
                if (baseResponseEntity.getCode() == null) {
                    return;
                }
                if (baseResponseEntity.getCode().equals("200")) {
                    submitMutableLiveData.setValue(baseResponseEntity.getCode());
                    return;
                }
                infoResultMsg = baseResponseEntity.getMessage();
                errorResultCode.setValue(infoResultMsg);
            }

            @Override
            public void onError(String error) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                infoResultMsg = error;
                errorResultCode.setValue(infoResultMsg);
            }
        });
    }
}
